import { Component, ViewChild, ElementRef } from '@angular/core';
import { IonicPage, NavController, NavParams } from 'ionic-angular';

//https://github.com/JiaXinYi/Ionic-Maps

// baidu map
declare var cordova;
declare var BMap;
declare var BMap_Symbol_SHAPE_POINT;
//BMAP_STATUS_SUCCESS        检索成功。对应数值“0”。
//BMAP_STATUS_CITY_LIST        城市列表。对应数值“1”。
//BMAP_STATUS_UNKNOWN_LOCATION        位置结果未知。对应数值“2”。
//BMAP_STATUS_UNKNOWN_ROUTE        导航结果未知。对应数值“3”。
//BMAP_STATUS_INVALID_KEY        非法密钥。对应数值“4”。
//BMAP_STATUS_INVALID_REQUEST        非法请求。对应数值“5”。
//BMAP_STATUS_PERMISSION_DENIED        没有权限。对应数值“6”。(自 1.1 新增)
//BMAP_STATUS_SERVICE_UNAVAILABLE        服务不可用。对应数值“7”。(自 1.1 新增)
//BMAP_STATUS_TIMEOUT        超时。对应数值“8”。(自 1.1 新增)



@IonicPage()
@Component({
  selector: 'page-baidumap',
  templateUrl: 'baidumap.html'
})
export class BaiDuMapPage {

  @ViewChild('map') map_container: ElementRef;
  map: any;//地图对象
  marker: any;//标记
  myIcon: any;
  Point:any  ;
  Latitude:any;

  constructor( ) {
    this.myIcon = new BMap.Icon("assets/icon/favicon.ico", new BMap.Size(30, 30));
    this.Point={};
    // 联通综合大楼 106.652004,26.653776
    this.Point.Longitude=106.652043;
    this.Point.Latitude=26.653758;
  }

  ionViewDidEnter() {
    let map =
      this.map =
        new BMap.Map(
          this.map_container.nativeElement,
          {
            enableMapClick: true,//点击拖拽
            enableScrollWheelZoom: true,//启动滚轮放大缩小，默认禁用
            enableContinuousZoom: true //连续缩放效果，默认禁用
          }
        );//创建地图实例

     this.viewlo();

  }

  viewmark(point) {
    this.map.clearOverlays();
    let marker = this.marker = new BMap.Marker(point, { icon: this.myIcon });
    this.map.addOverlay(marker);
    this.map.centerAndZoom(point, 16);
  }

  viewlo() {
    let point = new BMap.Point(this.Point.Longitude, this.Point.Latitude);
    //BMap.Convertor.translate(point, 0, function (point){   this.viewmark(point);  });     //真实经纬度转成百度坐标

    this.viewmark(point);
  }

  resetview() {
    // baidu 联通综合大楼 106.652049,26.653792
    this.Point.Longitude=106.652049;
    this.Point.Latitude=26.653792;
    this.viewlo();
  }

  googleview() {
    // google  26.647201, 106.645482
    this.Point.Longitude=106.645482;
    this.Point.Latitude=26.647201;
    this.viewlo();
  }

  calcview() {
    // 26.651152 106.641907
    this.Point.Longitude=106.641907;
    this.Point.Latitude=26.651152;
    this.viewlo();
  }

  randomview() {
    this.Point.Longitude=this.Point.Longitude + Math.random()/10000;
    this.Point.Latitude=this.Point.Latitude+ Math.random()/10000;
    this.viewlo();
  }


  whilesetlocal () {
    var _params = {};
    _params['Longitude'] = this.Point.Longitude;
    _params['Latitude'] = this.Point.Latitude;
    console.log("------------------------------callSomething：" +_params.toString() );
    var extraInfo = cordova.require('com.onluy.plugins.telemanager.telemanager');
    extraInfo.DoSome('WhileSetLocal', _params,
      function (message) {   },
      function (message) { alert(message); });
  }



}
